/*file:hash.h
 *an implement of a hashtable
 *
 */
#ifndef __HASH_H__
#define __HASH_H__

namespace misc {

typedef unsigned int (*GHashFunc)            (const void *key);
typedef bool         (*GEqualFunc)           (const void *a, const void *b);


bool g_str_equal (const void *v1, const void *v2);
unsigned int g_str_hash (const void *key);


struct GHashNode
{
    void   *key;
    void   *value;
    GHashNode *next;
};

class GHashTable
{
private:
    unsigned int     size;
    unsigned int     nnodes;
    GHashNode      **nodes;
    GHashFunc        hash_func;
    GEqualFunc       key_equal_func;
    //GDestroyNotify   key_destroy_func;
    //GDestroyNotify   value_destroy_func;

    void        g_hash_table_resize();
    GHashNode** g_hash_table_lookup_node(const void *key);
    GHashNode*  g_hash_node_new (void *key, void *value);
    void        g_hash_node_destroy (GHashNode *hash_node);
    void        g_hash_nodes_destroy (GHashNode *hash_node);
    unsigned int g_spaced_primes_closest (unsigned int num);


public:
    void *g_hash_table_lookup(const void *key);
    void g_hash_table_insert (void *key, void *value);
    bool g_hash_table_remove (const void *key);

    //default hashfunction is str_hash, default equalfunction is NULL.
    GHashTable(GHashFunc hash_func, GEqualFunc key_equal_func);
    ~GHashTable();
};

}//namespace misc

#endif /*__HASH_H__*/

